<template>
	<div class="">
	<avue-crud ref="crud"
		:page="page"
		:data="cashBackDataList"
		:table-loading="dataListLoading"
		:option="tableOption"
		v-model="obj"
		@on-load="getCashBack"
		@refresh-change="refreshChange">
	<template slot="menuLeft">
		<el-button type="primary" size="small" @click='addOrUpdateHandle'>新增</el-button>
	</template>

	<template slot="pic" slot-scope="scope">
		<el-image 
			style="width: 150px; "
			:src="scope.row.pic" 
			:preview-src-list="[scope.row.pic]">
		</el-image>      
	</template>	

	<template slot-scope="scope" slot="menu">
		<el-button icon="el-icon-edit" size="small" type="primary"  @click.stop="addOrUpdateHandle(scope.row)">编辑</el-button>
		<el-button icon="el-icon-delete" size="small" type="warning" @click.stop="deleteHandle(scope.row)">删除</el-button>
	</template>              
               
	<template slot="amount" slot-scope="row">
		{{row.row.type == 1?row.row.amount*100+'%':row.row.amount}}
	</template>

	<template slot="status" slot-scope="scope">
		<!-- {{scope.row.status}} -->          
		<el-switch v-model="scope.row.status" active-value="0" inactive-value="1" active-text="打开" inactive-text="关闭" active-color="#13ce66" @change="handleSwtich($event,scope.row)"></el-switch>
	</template>

	</avue-crud>
	<addOrUpdate 
		v-if="addOrUpdateVisible"
		ref="addOrUpdate"
		@refreshDataList="getCashBack"></addOrUpdate>
	</div>
</template>

<script>
import { tableOption } from '@/crud/prod/discountedProd.js'
import addOrUpdate from './discountedProd-add-or-update'

export default {
	data() {
		return {
			obj :{},
			addOrUpdateVisible :false,
			tableOption: tableOption,
			cashBackDataList: [],
			dataListLoading: false,
			page: {
				total: 0, // 总页数
				currentPage: 1, // 当前页数
				size: 10 // 每页显示多少条
			},
		}
	},

	components:{
		addOrUpdate
	},

	methods:{
		getCashBack(page,params){
			page = (page === undefined? this.page: page)
			this.dataListLoading = true
			this.$http({
				url: this.$http.adornUrl('/activity/page'),
				method: 'get',
				params: this.$http.adornParams(
					Object.assign({
						current: page == null? this.page.currentPage: page.currentPage,
						size: page == null? this.page.size: page.pageSize,
						},params)
				)
			}).then(({data}) =>{
				this.cashBackDataList = data.records
				this.page.total = data.total
				this.dataListLoading = false
			})
		},

		/**
		 * 刷新回调
		 */
		refreshChange () {
				this.getCashBack(this.page)
		},


		addOrUpdateHandle (row) {
			this.addOrUpdateVisible = true
			this.$nextTick(() => {
					this.$refs.addOrUpdate.init(row)
			})
			this.getCashBack()                            
		},

		deleteHandle(row){
			this.$confirm('您将删除此商品活动, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$http({
					url: this.$http.adornUrl('/activity/deleteActivity'),
					method: 'post',
					data: this.$http.adornParams(
						Object.assign({
								activityId	: row.activityId	
						},)
					)              
				}).then(({data}) => {
					this.getCashBack()
				})
				this.$message({
					type: 'success',
					message: '删除成功!'
				});
				}).catch(() => {
				this.$message({
						type: 'info',
						message: '已取消删除'
				});          
			});            
		},

	handleSwtich(value,row){
		console.log(row);
		this.$http({
			url: this.$http.adornUrl('/activity/updateActivityPro'),
			method: 'post',
			data: this.$http.adornParams({
				activityId: row.activityId,
				status: value,
				activityPrice: row.activityPrice,
				activityTimes: row.activityTimes,
			})              
		}).then(({data}) => {
			this.$message({
					message:'操作成功',
					type:'success'
			})
		})
	},

	},


}
</script>
<style lang="scss">
</style>

